class Topic < ActiveRecord::Base
  belongs_to :forum, :counter_cache => true
  belongs_to :user
  
  has_many :topic_replies
  
  validates_presence_of     :title, :message => "标题不能为空"
  validates_presence_of     :content, :message => "内容不能为空"
  validates_length_of       :title,    :within => 3..100, :too_long => "标题在3到100位之间", :too_short => "标题在3到100位之间"
  validates_length_of       :content,    :within => 5..5000, :too_long => "内容在5到5000位之间", :too_short => "内容在5到5000位之间"
  
  analyzer = Ferret::Analysis::PerFieldAnalyzer.new(Ferret::Analysis::StandardAnalyzer.new)
  analyzer[:title] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  analyzer[:content] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  
  acts_as_ferret({:fields => {:title => {:store => :yes}, :content => {:store => :yes}}}, {:analyzer=> analyzer})
  
  cattr_reader :per_page
  @@per_page = 15
  
  def user_has_auth_destroy?(user)
  	return true if user == self.user
  	if self.forum.class == CommunityForum && self.forum.community.can_edit?(user)
  	    return true
  	else
            return false
  	end
  end
  
  def before_save
    self.title = self.title.gsub(/^\<(script|frame)/i, '')
    self.content = self.content.gsub(/^\<(script|frame)/i, '')
  end
end
